package com.xxmw.transaction.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.common.excep.service.TransactionException;
import com.xxmw.transaction.common.utils.Assert;
import com.xxmw.transaction.mapper.app.UserBrickMapper;
import com.xxmw.transaction.model.app.BourseApi;
import com.xxmw.transaction.model.app.UserBrick;
import com.xxmw.transaction.model.app.UserBrickApi;
import com.xxmw.transaction.service.app.BourseApiService;
import com.xxmw.transaction.service.app.UserBrickApiService;
import com.xxmw.transaction.service.app.UserBrickService;
import com.xxmw.transaction.vo.UserBrickDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户搬砖策略服务
 * @author luowenliang
 * @since 2020/5/18 11:56
 */
@Slf4j
@Service
public class UserBrickServiceImpl extends ServiceImpl<UserBrickMapper, UserBrick> implements UserBrickService  {

    @Autowired
    private UserBrickApiService userBrickApiService;

    @Autowired
    private BourseApiService bourseApiService;

    @Override
    public List<UserBrick> findByBrickIds(List<Integer> brickIdList) {
        QueryWrapper<UserBrick> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("brick_id", brickIdList);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserBrick> findByBrickId(Integer brickId) {
//        QueryWrapper<UserBrick> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("brick_id", brickId);
//        queryWrapper.eq("status", 2);
//        return baseMapper.selectList(queryWrapper);
        return baseMapper.findByBrickId(brickId);
    }

    @Override
    public List<UserBrickDetailVo> findByUserId(Long userId, String symbol) {
        return baseMapper.findByUserIdAndSymbol(userId, symbol);
    }

    @Override
    @Transactional
    public void updateStatus(Long userId, Integer id, Integer status) {
        UserBrick userBrick = getById(id);
        if (Assert.isNullOrEmpty(userBrick) || !userBrick.getUserId().equals(userId)) {
            throw new TransactionException("策略不存在");
        }
        if (status.equals(2)) {
            // 将其它的同symbol策略关闭
            baseMapper.updataStatusToCloseBySymbol(userId, userBrick.getSymbol());
        }
        // 开启/关闭这个交易对
        userBrick.setStatus(status);
        updateById(userBrick);
    }

    @Override
    public void saveOrUpdate(UserBrick userBrick, String apiString, String username) {
        if (Assert.isNullOrEmpty(userBrick.getId())) {
            // 新增逻辑
            save(userBrick);
        } else {
            // 修改逻辑
            updateById(userBrick);
            HashMap<String, Object> map = new HashMap<>();
            map.put("user_brick_id", userBrick.getId());
            userBrickApiService.removeByMap(map);
        }
        String[] apis = apiString.split(",");
        ArrayList<Integer> apiList = new ArrayList<>();
        for (String api:
                apis) {
            apiList.add(Integer.valueOf(api));
        }
        List<BourseApi> bourseByApi = bourseApiService.getBourseByApi(username, apiList);
        List<UserBrickApi> userBrickApis = new ArrayList<>();
        for (BourseApi bourseApi : bourseByApi){
            UserBrickApi api = new UserBrickApi();
            api.setUserBrickId(userBrick.getId());
            api.setApiId(bourseApi.getId());
            api.setBourseId(bourseApi.getBourse());
            userBrickApis.add(api);
        }
        userBrickApiService.saveBatch(userBrickApis);
    }

    @Override
    public void closeUserBrickByVip() {
        baseMapper.closeUserBrickByVip();
    }
}
